<?php
/**
 * User: @yiwan
 * Date: 2019-09-10
 */

ini_set('display_errors', 1);
error_reporting(-1);

require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\trade\Order as mOrder;
use Biz\model\trade\OrderRefund as mOrderRefund;
use Biz\service\esSearch\OrderRefund as OrderRefundSearch;

$page = 1;
$pageSize = 2000;
do {
    $field = ['id', 'type_refund', 'status', 'reason', 'refund_order_no', 'order_no', 'uid', 'shop_id', 'express_no', 'addtime', 'confirm_time', 'rights_status', 'launch_rights_time','rights_into_time', 'rights_into_timeout_time'];
    $limit = [($page - 1) * $pageSize, $pageSize];
    $refundList = mOrderRefund::select($field, ['LIMIT' => $limit, 'ORDER' => ['addtime' => 'DESC']]);
    $sum = count($refundList);
    $orderNos = array_column($refundList, 'order_no');
    $orderNos = array_unique($orderNos);
    $orderList = mOrder::select(['order_no', 'status'], ['order_no' => $orderNos]);
    $orderStatusArr = [];
    foreach ($orderList as $row) {
        $orderStatusArr[$row['order_no']] = $row['status'];
    }
    $i=0;
    foreach ($refundList as $row) {
        $orderStatus = -1;
        if (isset($orderStatusArr[$row['order_no']])) {
            $orderStatus = $orderStatusArr[$row['order_no']];
        }
        $row['order_info'] = ['status' => $orderStatus];
        if ($row['addtime'] == '0000-00-00 00:00:00') {
            $row['addtime'] = '0000-01-30 00:00:00';
        }
        if ($row['confirm_time'] == '0000-00-00 00:00:00') {
            $row['confirm_time'] = '0000-01-30 00:00:00';
        }
        if ($row['launch_rights_time'] == '0000-00-00 00:00:00') {
            $row['launch_rights_time'] = '0000-01-30 00:00:00';
        }
        if ($row['rights_into_timeout_time'] == '0000-00-00 00:00:00') {
            $row['rights_into_timeout_time'] = '0000-01-30 00:00:00';
        }
        if ($row['rights_into_time'] == '0000-00-00 00:00:00') {
            $row['rights_into_time'] = '0000-01-30 00:00:00';
        }
        $result = OrderRefundSearch::saveByOrderRefund($row);
        if ($result['code'] != 1) {
            var_dump($result);
        }
        echo "总数据$sum,已经同步" . $i++;
    }
    $page++;
} while ($refundList);

